package com.wzh.codegen.biz.controller;

import com.wzh.codegen.biz.domain.dto.CodegenTableDTO;
import com.wzh.codegen.biz.service.CodegenService;
import com.wzh.common.general.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 数据库表代码生成控制器
 *
 * @author wzh
 * @since 2024-01-01
 */
@Tag(name = "数据库表代码生成", description = "数据库表代码生成相关接口")
@RestController
@RequestMapping("/table")
@RequiredArgsConstructor
public class CodegenTableController {

    private final CodegenService codegenService;

    // ==================== 表管理接口 ====================

    @Operation(summary = "获取数据库表列表")
    @GetMapping("/list/{datasourceId}")
    public Result<List<Map<String, Object>>> getTables(@PathVariable("datasourceId") Long datasourceId) {
        List<Map<String, Object>> tables = codegenService.getTables(datasourceId);
        return Result.success(tables);
    }

    @Operation(summary = "获取表字段信息")
    @GetMapping("/columns/{datasourceId}/{tableName}")
    public Result<List<Map<String, Object>>> getColumns(
            @PathVariable("datasourceId") Long datasourceId,
            @PathVariable("tableName") String tableName) {
        List<Map<String, Object>> columns = codegenService.getColumns(datasourceId, tableName);
        return Result.success(columns);
    }

    @Operation(summary = "创建数据库表")
    @PostMapping("/create")
    public Result<Void> createTable(@Valid @RequestBody CodegenTableDTO dto) {
        codegenService.createTable(dto);
        return Result.success();
    }

    @Operation(summary = "同步数据库表结构")
    @PostMapping("/sync/{datasourceId}/{tableName}")
    public Result<Void> syncTable(@PathVariable("datasourceId") Long datasourceId, @PathVariable("tableName") String tableName) {
        codegenService.syncTable(datasourceId, tableName);
        return Result.success();
    }

    @Operation(summary = "同步表结构")
    @PostMapping("/sync/structure/{datasourceId}/{tableId}")
    public Result<List<Map<String, Object>>> syncTableStructure(
            @PathVariable("datasourceId") Long datasourceId,
            @PathVariable("tableId") Long tableId) {
        List<Map<String, Object>> columns = codegenService.syncTableStructure(datasourceId, tableId);
        return Result.success(columns);
    }

    @Operation(summary = "获取通用字段")
    @GetMapping("/common/fields")
    public Result<List<Map<String, Object>>> getCommonFields() {
        List<Map<String, Object>> fields = codegenService.getCommonFields();
        return Result.success(fields);
    }

    // ==================== 代码生成接口 ====================

    @Operation(summary = "生成完整表代码")
    @PostMapping("/generate")
    public Result<String> generateTable(@Valid @RequestBody CodegenTableDTO dto) {
        String result = codegenService.generateTable(dto);
        return Result.success(result);
    }

    @Operation(summary = "下载表代码压缩包")
    @PostMapping("/download")
    public ResponseEntity<Resource> downloadTable(@Valid @RequestBody CodegenTableDTO dto) {
        return codegenService.downloadTable(dto);
    }

    @Operation(summary = "同步代码")
    @PostMapping("/sync/code")
    public Result<Void> syncCode(@Valid @RequestBody CodegenTableDTO dto) {
        codegenService.syncCode(dto);
        return Result.success();
    }

    // ==================== 单个文件生成接口 ====================

    @Operation(summary = "生成实体类代码")
    @PostMapping("/generate/entity")
    public Result<String> generateEntity(@RequestBody CodegenTableDTO tableDTO) {
        String entityCode = codegenService.generateEntity(tableDTO);
        return Result.success(entityCode);
    }

    @Operation(summary = "生成DTO代码")
    @PostMapping("/generate/dto")
    public Result<String> generateDTO(@RequestBody CodegenTableDTO tableDTO) {
        String dtoCode = codegenService.generateDTO(tableDTO);
        return Result.success(dtoCode);
    }

    @Operation(summary = "生成Mapper代码")
    @PostMapping("/generate/mapper")
    public Result<String> generateMapper(@RequestBody CodegenTableDTO tableDTO) {
        String mapperCode = codegenService.generateMapper(tableDTO);
        return Result.success(mapperCode);
    }

    @Operation(summary = "生成Service代码")
    @PostMapping("/generate/service")
    public Result<String> generateServiceCode(@RequestBody CodegenTableDTO tableDTO) {
        String serviceCode = codegenService.generateServiceCode(tableDTO);
        return Result.success(serviceCode);
    }

    @Operation(summary = "生成ServiceImpl代码")
    @PostMapping("/generate/serviceImpl")
    public Result<String> generateServiceImpl(@RequestBody CodegenTableDTO tableDTO) {
        String serviceImplCode = codegenService.generateServiceImpl(tableDTO);
        return Result.success(serviceImplCode);
    }

    @Operation(summary = "生成Controller代码")
    @PostMapping("/generate/controller")
    public Result<String> generateController(@RequestBody CodegenTableDTO tableDTO) {
        String controllerCode = codegenService.generateController(tableDTO);
        return Result.success(controllerCode);
    }

    @Operation(summary = "生成SQL代码")
    @PostMapping("/generate/sql")
    public Result<String> generateSql(@RequestBody CodegenTableDTO tableDTO) {
        String sqlCode = codegenService.generateSql(tableDTO);
        return Result.success(sqlCode);
    }
}